package com.gary.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gary.demo.biz.ProductBO;
import com.gary.demo.entity.Product;
import com.gary.demo.model.param.ProductOperateParam;
import com.gary.demo.model.param.ProductQueryParam;
import com.gary.demo.vo.ProductVO;
import com.gary.demo.vo.param.ProductOperateParamVO;
import com.gary.demo.vo.param.ProductQueryParamVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductBO productBO;


    @PostMapping("/add")
    public Integer add(@RequestBody ProductOperateParamVO paramVO) {
        ProductOperateParam param = new ProductOperateParam();
        BeanUtils.copyProperties(paramVO, param);
        return productBO.add(param);
    }

    @PostMapping("/update")
    public void update(@RequestBody ProductOperateParamVO paramVO) {
        ProductOperateParam param = new ProductOperateParam();
        BeanUtils.copyProperties(paramVO, param);
        productBO.update(param);
    }


    @GetMapping("/pageQuery")
    public IPage<ProductVO> pageQuery(ProductQueryParamVO paramVO) {
        ProductQueryParam param = new ProductQueryParam();
        BeanUtils.copyProperties(paramVO, param);
        IPage<Product> goodsIPage = productBO.pageQuery(param);
        List<ProductVO> productVOList = goodsIPage.getRecords().stream().map(item -> {
            ProductVO goodsVO = new ProductVO();
            BeanUtils.copyProperties(item, goodsVO);
            return goodsVO;
        }).collect(Collectors.toList());
        Page<ProductVO> goodsVOPage = new Page<>();
        BeanUtils.copyProperties(goodsIPage, goodsVOPage);
        goodsVOPage.setRecords(productVOList);
        return goodsVOPage;
    }

}
